package com.amigo.online.provider.adv.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.amigo.online.common.param.adv.bo.AdvTantanInitBO;
import com.amigo.online.common.param.result.AdvInfo;
import com.amigo.online.provider.adv.common.ConstantValue;
import com.amigo.online.provider.adv.mapper.AdvTantanApplyMapper;
import com.amigo.online.provider.adv.service.AdvTantanApplyService;
import com.amigo.online.provider.adv.service.AdvTantanSourceService;
import com.amigo.online.provider.adv.service.common.AbstractAdvApplyService;
import com.amigo.online.provider.adv.util.AdvBasicUtil;

@Service
@SuppressWarnings("unchecked")
public class AdvTantanApplyServiceImpl extends AbstractAdvApplyService implements AdvTantanApplyService {

    @Autowired
    AdvTantanApplyMapper advTantanApplyMapper;

    @Autowired
    AdvTantanSourceService advTantanSourceService;

    @Override
    public List<AdvTantanInitBO> listFindTodayUsableResourceByTime(long currentMillis) {

        return advTantanApplyMapper.listFindTodayUsableResourceByTime(currentMillis);
    }

    @Override
    public List<AdvTantanInitBO> listFindTomorrowAdvDeliveryPlanByStartTime(long tomorrow) {
        return advTantanApplyMapper.listFindTomorrowAdvDeliveryPlanByStartTime(tomorrow);
    }

    @Override
    public List<AdvTantanInitBO> listFindTomorrowAdvSettlePlanByEndTime(long today) {
        return advTantanApplyMapper.listFindTomorrowAdvSettlePlanByEndTime(today);
    }

    @Override
    public List<AdvInfo> listHomeAdvByAreaIdAndSexTypeAndAgeType(Integer sexType, Integer ageType, Double lng, Double lat,
                                                                 Integer areaId, int number, double ratio) {
        List<AdvInfo> result = new ArrayList<>();
        Set<Integer> advSet = new HashSet<>(32);
        List<Integer> cityAdv = listHomeAdvByCity(areaId, sexType, ageType, number);
        List<Integer> areaAdv = listHomeAdvByPoint(lng, lat, sexType, ageType, number);
        advSet.addAll(cityAdv);
        advSet.addAll(areaAdv);
        if (advSet.size() != 0) {
            List<Integer> remainingList = rwRedisService.opsForHashRead().multiGet(ConstantValue.ADV_TANTAN_STOCK, advSet);
            advSet = getRandomAdvIds(advSet, remainingList, number);
            if (advSet.size() != 0) {
                result = advTantanSourceService.getAdvSourceBySizeAndIds(ratio, advSet);
            }
        }
        return result;
    }

    private List<Integer> listHomeAdvByCity(int areaId, int sexType, int ageType, int number) {
        // 区域
        String redisKey = AdvBasicUtil.spliceAdvRedisKey(ConstantValue.ADV_TANTAN_LIST, areaId, sexType, ageType);
        // 获取当前城市广告
        List<Integer> areaAdvList = rwRedisService.opsForListRead().range(redisKey, 0, -1);
        if (areaAdvList.size() < number && areaId != 0) {
            // 区域的不够 ，全国补充
            String globalRedisKey = AdvBasicUtil.spliceAdvRedisKey(ConstantValue.ADV_TANTAN_LIST, 0, sexType,
                    ageType);
            areaAdvList.addAll(rwRedisService.opsForListRead().range(globalRedisKey, 0, -1));
        }
        return areaAdvList;
    }

    private List<Integer> listHomeAdvByPoint(Double lng, Double lat, int sexType, int ageType, int number) {
        List<Integer> result = new ArrayList<>();
        return result;
    }


}
